package cn.vawn.exam.service.impl;

import cn.vawn.base.dto.PageDTO;
import cn.vawn.base.dto.RequestPageDTO;
import cn.vawn.exam.entity.PaperType;
import cn.vawn.exam.mapper.PaperTypeMapper;
import cn.vawn.exam.service.IPaperTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("paperTypeService")
public class PaperTypeServiceImpl implements IPaperTypeService {

    @Resource
    private PaperTypeMapper paperTypeMapper;

    @Override
    public PageDTO searchPaperType(RequestPageDTO requestPageDTO, String typeName, Date startTime, Date endTime) {

        PageDTO pd = new PageDTO();

        int pageNumber = requestPageDTO.getPageNumber();
        int pageSize = requestPageDTO.getPageSize();
        String sort = requestPageDTO.getSort();
        String desc = requestPageDTO.getDesc();
        if (pageNumber > 0 && pageSize > 0) {
            PageHelper.startPage(pageNumber, pageSize);
        }
        if (!sort.isEmpty() && !desc.isEmpty()) {
            PageHelper.orderBy(sort + " " + desc);
        }

        List<PaperType> searchList = paperTypeMapper.searchPaperType(typeName, startTime, endTime);

        pd.setPageNumber(pageNumber);
        pd.setPageSize(pageSize);
        pd.setTotal((int) new PageInfo(searchList).getTotal());
        pd.setData(new PageInfo(searchList).getList());

        return pd;
    }

    @Override
    public PaperType selectById(Integer id) {
        return paperTypeMapper.selectById(id);
    }

    @Override
    public int insert(PaperType paperType) {
        return paperTypeMapper.insert(paperType);
    }

    @Override
    public int update(PaperType paperType) {
        return paperTypeMapper.update(paperType);
    }

    @Override
    public void delete(PaperType paperType) {
        paperTypeMapper.delete(paperType);
    }
}
